// 防抖
export const debounce = (func, wait) => {
  let timeout;
  return function(...args) {
    const context = this;
    clearTimeout(timeout);
    timeout = setTimeout(() => func.apply(context, args), wait);
  };
}
// 小数精度问题
export const precisionCalc = (num1, num2, operator) => {
  const maxDecimal = Math.max(
    (num1.toString().split('.')[1] || '').length,
    (num2.toString().split('.')[1] || '').length
  )
  const base = Math.pow(10, maxDecimal)
  
  const n1 = Math.round(num1 * base)
  const n2 = Math.round(num2 * base)
  
  switch(operator) {
    case '+': return (n1 + n2) / base
    case '-': return (n1 - n2) / base
    case '*': return (n1 * n2) / (base * base)
    case '/': return (n1 / n2) * base
    default: throw new Error('无效运算符')
  }
}
// 填充数据
export const setLocalData = (formData)=> {
	return new Promise((resolve,reject) => {
		if(!formData.time) reject('时间丢失,请重新尝试')
		const timeSplit = formData.time.split('-')
		const year = timeSplit[0],
			  month = timeSplit[1],
			  day = timeSplit[2]
		let data = uni.getStorageSync(`data-${year}`)
		if(data){
			// 查看有没有月
			const monthIndex = data.findIndex(item => item.month === month)
			// 有月 
			if(monthIndex != -1){
				// 查看 有没有日
				const dayIndex = data[monthIndex].children.findIndex(item => item.time.split('-')[2] === day)
				// 有日覆盖
				if(dayIndex != -1){
					data[monthIndex].children[dayIndex] = formData
				} else {
				// 没有填充
					data[monthIndex].children.push(formData)	
				}
			} else {
			// 没有月直接赋值
				data.push({
					month,
					children: [
						formData
					]
				})
			}
		}else {
			data = [
				{
					month,
					children: [
						formData
					]
				}
			]
		}
		// 保存
		uni.setStorage({
			key: `data-${year}`,
			data: data,
			success: function () {
				resolve('success')
			},
			fail: function () {
				reject('保存失败,请检查保存状态后重新尝试')
			}
		});
	})
}
// 查询数据
export const getLocalData = (date, type = 'day')=> {
	const year = date.split('-')[0],
		  month = date.split('-')[1],
		  day = date.split('-')[2]
	const yearData = uni.getStorageSync(`data-${year}`)
	const monthData = yearData ? yearData.find(item => item.month === month) : { month: month , children: [] }
	const dayData = monthData && monthData.children.length > 0 ? monthData.children.find(item => item.time.split('-')[2] === day) : {}
	return type === 'day' ? dayData : monthData
}
// 删除数据
export const delLocalData = (date) => {
	return new Promise((resolve,reject) => {
		const year = date.split('-')[0],
			  month = date.split('-')[1],
			  day = date.split('-')[2]
		let data = uni.getStorageSync(`data-${year}`)
		if(data){
			// 查看有没有月
			const monthIndex = data.findIndex(item => item.month === month)
			// 有月 
			if(monthIndex != -1){
				// 查看 有没有日
				const dayIndex = data[monthIndex].children.findIndex(item => item.time.split('-')[2] === day)
				// 有日覆盖
				if(dayIndex != -1){
					data[monthIndex].children.splice(dayIndex, 1)
				}else {
					reject('没有当日数据!')
				}
			}else {
				reject('没有当月数据!')
			}
		}else {
			reject('没有当年数据!')
		}
		console.log(data);
		// 保存
		uni.setStorage({
			key: `data-${year}`,
			data: data,
			success: function () {
				resolve('删除成功')
			},
			fail: function () {
				reject('删除失败')
			}
		});
	})
}